﻿using System;
using System.Linq.Expressions;

namespace Core
{
    public class Nameof<T>
    {
        public static string Member<TProp>(Expression<Func<T, TProp>> expression)
        {
            var body = expression.Body as MemberExpression;
            if (body == null)
                throw new ArgumentException("'expression' should be a member expression");
            return body.Member.Name;
        }
    }

    public static class NameOfHelper
    {
        public static string Member<T, TProp>(this T obj, Expression<Func<T, TProp>> expression)
        {
            var memberExp = expression.Body as MemberExpression;
            if (memberExp != null)
                return memberExp.Member.Name;

            var methodExp = expression.Body as MethodCallExpression;
            if (methodExp != null)
                return methodExp.Method.Name;

            throw new ArgumentException("'expression' should be a member expression or a method call expression.", "expression");
        }

        public static string Member<TProp>(Expression<Func<TProp>> expression)
        {
            var memberExp = expression.Body as MemberExpression;
            if (memberExp != null)
                return memberExp.Member.Name;

            var methodExp = expression.Body as MethodCallExpression;
            if (methodExp != null)
                return methodExp.Method.Name;

            throw new ArgumentException("'expression' should be a member expression or a method call expression.", "expression");
        }
    }
}